
let nunjucks = require('nunjucks');

function createEnv(view, opts) {
    view = view || "views";
    opts = opts || {};

    optsObj = {
        autoescape: opts.autoescape || true,
        throwOnUndefined: opts.throwOnUndefined || false,
        trimBlocks: opts.trimBlocks || false,
        lstripBlocks: opts.lstripBlocks || false,
        watch: opts.watch || false,
        noCache: opts.noCache || true
    }

    return nunjucks.configure(view, optsObj)
}

module.exports = async (ctx, next) => {
    ctx.render = function (views, obj) {
        let envs = createEnv();
        ctx.body = envs.render(views, obj);
    }

    await next();
}